<template>
    <div class="login-bg">
        <div class="login-box">
            <!-- 头像 -->
            <div class="avatar">
                <img src="../assets/logo.png" alt="">
            </div>
            <!-- 表单 -->
            <el-form class="login-form" :model="loginForm" :rules="loginFormRules" ref="LoginFormRef">
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" prefix-icon="iconfont icon-3702mima" type="password"></el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="loginFormReset">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            loginForm: {
                username: 'admin',
                password: '123456'
            },
            // 表单的验证规则对象
            loginFormRules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 10, message: '长度应该在 3 到 10 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入登录密码', trigger: 'blur' },
                    { min: 3, max: 10, message: '长度应该在 6 到 15 个字符', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        loginFormReset() {
            console.log(this.$refs);
            this.$refs.LoginFormRef.resetFields();
        },
        login() {
            this.$refs.LoginFormRef.validate(async valid => {
                // console.log(valid);
                if (!valid) return; // 验证不通过
                const { data: res } = await this.$http.post('login', this.loginForm);
                if (res.meta.status !== 200) {
                    this.$message.error(res.meta.msg);
                } else {
                    this.$message.success(res.meta.msg);
                }
                console.log(res);
                // 将 token 存储到 sessionStorage 域中
                window.sessionStorage.setItem('token', res.data.token);
                // 跳转到 home 页面
                this.$router.push('/home');
            });
        }
    }
}
</script>

<style lang="less" scoped>
.login-bg {
    height: 100%;
    background-color: #2b4b6b;
}
.login-box {
    width: 450px;
    height: 300px;
    border-radius: 3px;
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    .avatar {
        width: 130px;
        height: 130px;
        border-radius: 50%;
        background-color: #fff;
        box-shadow: 0 0 10px #fff;
        position: absolute;
        left: 50%;
        top: 0;
        transform: translate(-50%, -50%);
        padding: 5px;
        border: 1px solid #eee;
        img {
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background-color: #eee;
        }
    }
}
.login-form {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 0 20px;
    .btns {
        display: flex;
        justify-content: flex-end;
    }
}
</style>
